/**
 * Project Name:mauto-web
 * File Name:AreaService.java
 * Package Name:com.inftech.mauto.sys.service
 * Date:2016年10月7日下午9:14:13
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.inftech.mauto.common.constant.BusinessConstant;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.sys.dao.ICustomerServiceDao;
import com.inftech.mauto.sys.entity.CustomerService;
import com.mingsoft.base.biz.impl.BaseBizImpl;
import com.mingsoft.base.dao.IBaseDao;
import com.mingsoft.weixin.dao.IWeixinDao;
import com.mingsoft.weixin.entity.WeixinEntity;
import com.mingsoft.weixin.util.BaseUtils;
import com.mingsoft.weixin.util.KfSessionUtils;

/**
 * ClassName:CustomerServiceService <br/>
 * Function: 客服业务service. <br/>
 * Date: 2016年10月7日 下午9:14:13 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service("customerServiceService")
public class CustomerServiceService extends BaseBizImpl {

	/**
	 * 客服持久化层注入
	 */
	@Autowired
	private ICustomerServiceDao customerServiceDao;

	/**
	 * 注入微信持久化层
	 */
	@Autowired
	private IWeixinDao weixinDao;

	/**
	 * 后台服务器地址
	 */
	@Value("${adminUrl}")
	private String adminUrl;

	@Override
	protected IBaseDao getDao() {
		return customerServiceDao;
	}

	/**
	 * 
	 * queryCustomerServiceListFront:查询客服列表. <br/>
	 *
	 * @author puyf
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public List<Map<String, Object>> queryCustomerServiceListFront() throws Exception {
		// 获取微信信息
		WeixinEntity weixin = weixinDao.getEntity(null, null, null);
		KfSessionUtils kfSessionUtils = new KfSessionUtils(weixin.getWeixinAppID(), weixin.getWeixinAppSecret());
		// 获取微信方客服列表
		List<Map<String, Object>> kfList = kfSessionUtils.getKfList();
		// 获取数据库客服列表
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("objectType", BusinessConstant.FILE_TYPE_KF_IMG);
		List<CustomerService> customerServiceList = customerServiceDao.queryCustomerServiceList(paramMap);
		// 返回列表
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (customerServiceList != null && customerServiceList.size() != 0) {
			for (CustomerService customerService : customerServiceList) {
				Map<String, Object> customerServiceM = new HashMap<String, Object>();
				// 客服id
				customerServiceM.put("id", customerService.getId());
				// 客服帐号
				customerServiceM.put("csAccount", customerService.getCsAccount());
				// 客服名称
				customerServiceM.put("csName", customerService.getCsName());
				// 客服评分
				customerServiceM.put("csScore", customerService.getCsScore());
				// 客服头像地址
				if (!CommonUtils.isBlank(customerService.getCsImg())) {
					customerServiceM.put("csImg", adminUrl + customerService.getCsImg());
				}
				for (Map<String, Object> kfMap : kfList) {
					if (customerService.getCsAccount().equals(kfMap.get(BaseUtils.KF_ACCOUNT))) {
						// 客服状态
						customerServiceM.put("csStatus", Integer.valueOf(kfMap.get(BaseUtils.KF_STATUS).toString()));
						// 车大夫头像地址
						// carDoctorM.put("cdHeadimgurl",
						// kfMap.get(BaseUtils.KF_HEADIMGURL).toString());
						break;
					}
				}
				resultList.add(customerServiceM);
			}
		}
		return resultList;
	}

	/**
	 * 
	 * queryCustomerServiceList:后台查询客服列表. <br/>
	 * 
	 * @author puyf
	 * @param paramMap
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public List<CustomerService> queryCustomerServiceList(Map<String, Object> paramMap) throws Exception {
		// 获取客服列表
		paramMap.put("objectType", BusinessConstant.FILE_TYPE_KF_IMG);
		List<CustomerService> customerServiceList = customerServiceDao.queryCustomerServiceList(paramMap);
		return customerServiceList;
	}

	/**
	 * 
	 * queryCustomerService:查询客服实体. <br/>
	 *
	 * @author puyf
	 * @param id
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, Object> queryCustomerService(int id) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		paramMap.put("objectType", BusinessConstant.FILE_TYPE_KF_IMG);
		// 获取客服实体
		CustomerService customerService = customerServiceDao.queryCustomerService(paramMap);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 返回对象
		if (customerService != null) {
			// 客服帐号
			resultMap.put("csAccount", customerService.getCsAccount());
			// 客服名称
			resultMap.put("csName", customerService.getCsName());
			// 客服评分
			resultMap.put("csScore", customerService.getCsScore());
			// 客服头像地址
			if (!CommonUtils.isBlank(customerService.getCsImg())) {
				resultMap.put("csImg", adminUrl + customerService.getCsImg());
			}
		}
		return resultMap;
	}
}
